package com.weayer.controller;

import com.weayer.dto.ResponseResult;
import com.weayer.dto.TokenResult;
import com.weayer.request.OrderRequest;
import com.weayer.service.ApiDriverOrderInfoService;
import com.weayer.util.JwtUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: Api服务 - 司机 - 订单Controller
 * @author: Weayer
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    ApiDriverOrderInfoService apiDriverOrderInfoService;

    @PostMapping("/grab")
    @GlobalTransactional
    public ResponseResult grab(@RequestBody OrderRequest order, HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("Authorization");
        // 从token中获取司机信息
        TokenResult tokenResult = JwtUtils.parseToken(token);
        String identity = tokenResult.getIdentity();
        String driverPhone = tokenResult.getPhone();

        Long orderId = order.getOrderId();
        String receiveOrderCarLongitude = order.getReceiveOrderCarLongitude();
        String receiveOrderCarLatitude = order.getReceiveOrderCarLatitude();

        return apiDriverOrderInfoService.grab(driverPhone, orderId, receiveOrderCarLongitude, receiveOrderCarLatitude);
    }

}
